<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="__STATIC__/plugins/layui/css/layui.css" media="all">
</head>
<body>
<script src="__STATIC__/plugins/layui/layui.js"></script>
<form method="POST" action="" accept-charset="UTF-8" class="layui-form">
    <input type="hidden" name="qids" id="npm">
    <div class="layui-form-item"><label for="name" class="layui-form-label">问卷名称</label>
        <div class="layui-input-block"><input  lay-verify="required" class="layui-input" id="name" name="name" type="text" value=""></div>
    </div>
    <div class="layui-form-item" >
        <table id="demo" lay-filter="demo"></table>
    </div>

    <!---->
    <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
        <button class="layui-btn layui-btn-primary" type="reset"><a class="layui-layer-btn1">取消</a></button>
    </div>
</form>
</body>
<script>
    layui.use(['form','table'], function () {
        var form = layui.form;
        var $ = layui.$;
        var table=layui.table;

        form.on('submit(formDemo)',function(data){
            $('#npm').val(localStorage.getItem('ids'));
            localStorage.removeItem('ids');
        });

        table.render({
                elem: '#demo'
                ,height: 315
                ,url: '{:url("question/questionSelect")}' //数据接口
                ,where:{id:0}
            ,cols: [[ //表头
            {checkbox: true, fixed: true}
            ,{field: 'name', title: '问题名称', width:200}
            ,{field: 'type', title: '类型', width:80, sort: true}
        ]]
            ,done:function(res){
            var init=[];
            res.data.forEach(function(v){
                if(v.LAY_CHECKED){
                    init.push(v.id);
                }
            });
            console.log(init);
            localStorage.setItem('ids',init.join(','));
        }
    });
        table.on('checkbox(demo)',function(obj){
            console.log(obj.data.id);
            var ids=localStorage.getItem('ids');
            ids=(ids)?ids.split(','):[];
            if(obj.checked){
                ids.push(obj.data.id);
            }else{
                var index=ids.indexOf(obj.data.id);
                ids.splice(index,1);
            }
            console.log(ids);
            localStorage.setItem('ids',ids.join(','));
        });
    });
</script>
</html>;